#include "register.h"
#include "ui_register.h"

Register::Register(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Register)
{
    ui->setupUi(this);
    this->ui->radioButton->setChecked(false);
    this->ui->register_pushButton->setEnabled(false);
    this->ui->nickname_content_label->hide();
    this->ui->password_content_label->hide();
    this->ui->password_confirm_content_label->hide();
}

Register::~Register()
{
    delete ui;
}

void Register::on_radioButton_clicked(bool checked)
{
    this->nickname = this->ui->nickname_lineEdit->text();
    this->password = this->ui->password_lineEdit->text();
    this->confirmPassword = this->ui->password_confirm_lineEdit->text();
    if (checked && this->nickname != nullptr && this->password != nullptr && this->confirmPassword != nullptr &&
            this->nicknameContent == nullptr && this->passwordContent == nullptr && this->confirmPasswordContent == nullptr &&
            this->nicknameContent.length() == 0 && this->passwordContent.length() == 0 && this->confirmPasswordContent.length() == 0)
    {
        this->ui->register_pushButton->setStyleSheet("background-color: rgb(0, 141, 235)");
        this->ui->register_pushButton->setEnabled(true);
    }
    else
    {
        this->ui->register_pushButton->setStyleSheet("background-color: rgb(158, 218, 255)");
        this->ui->register_pushButton->setEnabled(false);
    }
}

void Register::on_register_pushButton_clicked()
{
    // 生成随机账号
    QMessageBox::information(this, "注册成功", "");
}

void Register::on_nickname_lineEdit_textChanged(const QString &nickname)
{
    if (nickname == nullptr || nickname.length() == 0)
        this->nicknameContent = "❗昵称不可以为空";
    else if (nickname.length() > 12)
        this->nicknameContent = "❗昵称长度不能超过12";
    else
        this->nicknameContent.clear();
    if (this->nicknameContent.length() == 0)
    {
        this->nickname = nickname;
        this->ui->nickname_content_label->hide();
    }
    else
    {
        this->ui->nickname_content_label->show();
        this->ui->nickname_content_label->setText(this->nicknameContent);
        QString nicknameContentStyle = "color: red;";
        this->ui->nickname_content_label->setStyleSheet(nicknameContentStyle);
    }
}

void Register::on_password_lineEdit_textChanged(const QString &password)
{
    if (password == nullptr || password.length() == 0)
        this->passwordContent = "❗密码不可以为空";
    else if (nickname.length() > 16 || password.length() < 8)
    {
        this->passwordContent = "❗长度为8-16个字符";
        this->password = password;
    }
    else
        this->passwordContent.clear();
    if (this->passwordContent.length() == 0)
    {
        this->password = password;
        this->ui->password_content_label->hide();
    }
    else
    {
        this->ui->password_content_label->show();
        this->ui->password_content_label->setText(this->passwordContent);
        QString passwordContentStyle = "color: red;";
        this->ui->password_content_label->setStyleSheet(passwordContentStyle);
    }
}

void Register::on_password_confirm_lineEdit_textChanged(const QString &confirmPassword)
{
    if (confirmPassword == nullptr || confirmPassword.length() == 0 || this->password != confirmPassword)
        this->confirmPasswordContent = "❗两次密码不一致";
    else
        this->confirmPasswordContent.clear();
    if (this->confirmPasswordContent.length() == 0)
    {
        this->confirmPassword = confirmPassword;
        this->ui->password_confirm_content_label->hide();
    }
    else
    {
        this->ui->password_confirm_content_label->show();
        this->ui->password_confirm_content_label->setText(this->confirmPasswordContent);
        QString confirmPasswordContentStyle = "color: red;";
        this->ui->password_confirm_content_label->setStyleSheet(confirmPasswordContentStyle);
    }
}
